package com.hx.setting.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
public class TimeUtil
{

	public static final String	FORMAT_DATE_ONLY_CH	= "yyyy年MM月dd日";

	public static final String	FORMAT_MONTH_ONLY	= "yyyy-MM";

	public static final String	FORMAT_DATE_ONLY	= "yyyy-MM-dd";

	public static final String	FORMAT_DATE_HOUR	= "yyyy-MM-dd HH:mm";

	public static final String	FORMAT_TIME_ONLY	= "HH:mm:ss";

	public static final String	FORMAT_COMPACT		= "yyMMddHHmmssSSS";

	public static final String	FORMAT_NORMAL		= "yyyy-MM-dd HH:mm:ss";

	public static final String	FORMAT_DETAIL		= "yyyy-MM-dd HH:mm:ss.SSS";

	public static final String	FORMAT_YEAR			= "yyyy";

	public static final String	FORMAT_TIME_HOUR	= "HH";

	public static final String	FORMAT_TIME_MINUTE	= "mm";

	/** 一天有86400秒 */
	public static final long	DATE_SECOND			= 86400;

	/** 一天有1440分 */
	public static final long	DATE_MINUTE			= 1440;

	public static final long	MINUTE_SECOND		= 60;							/**/

	public static Date parse(String str, String format)
	{
		try
		{
			SimpleDateFormat sf = new SimpleDateFormat( format );
			return sf.parse( str );
		}
		catch ( ParseException e )
		{
			//e.printStackTrace();
			return null;
		}
	}

	public static String format(String format)
	{
		return format( new Date() ,format );
	}

	public static String format(Date date, String format)
	{
		try
		{
			SimpleDateFormat sf = new SimpleDateFormat( format );
			return sf.format( date );
		}
		catch ( Exception e )
		{
			return null;
		}
	}

	public static boolean isExpire(String strTime, String strExpiredTime)
	{
		Date time = parse( strTime ,FORMAT_NORMAL );
		Date expiredTime = parse( strExpiredTime ,FORMAT_NORMAL );
		return (time.compareTo( expiredTime ) >= 0);
	}

	/** 
	 * 生成制定日期的Date对象，从0点开始
	 * 
	 * @param year
	 * @param month
	 * @param days
	 * @return
	 */
	public static Date createDate(int year, int month, int days)
	{
		Calendar cal = Calendar.getInstance();
		cal.set( year ,month - 1 ,days ,0 ,0 ,0 );
		cal.set( Calendar.MILLISECOND ,0 );
		return cal.getTime();
	}

	/** 
	 * 计算时间差
	 * 
	 * @param beginTime
	 * 开始时间，格式：yyyy-MM-dd HH:mm:ss
	 * @param endTime
	 * 结束时间，格式：yyyy-MM-dd HH:mm:ss
	 * @return 从开始时间到结束时间之间的时间差（秒）
	 */
	public static long getTimeDifference(String beginTime, String endTime)
	{
		long between = 0;
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd" );
		Date end = null;
		Date begin = null;
		try
		{// 将截取到的时间字符串转化为时间格式的字符串
			end = sdf.parse( endTime );
			begin = sdf.parse( beginTime );
		}
		catch ( ParseException e )
		{
			e.printStackTrace();
		}
		between = (end.getTime() - begin.getTime()) / 1000;// 除以1000是为了转换成秒
		return between;
	}

	public static String getTimeDifference(Date beginTime, Date endTime)
	{
		if ( beginTime == null || endTime == null )
		{
			return "未知";
		}
		long between = endTime.getTime() - beginTime.getTime();
		if ( between <= 0 )
		{
			return null;
		}
		between = between / 1000;// 除以1000是为了转换成秒
		long date = between / DATE_SECOND;
		long hour = (between - date * DATE_SECOND) / 3600;
		long minute = (between - date * DATE_SECOND - hour * 3600) / 60;
		long sec = (between - date * DATE_SECOND - hour * 3600 - minute * 60);
		return date + "天" + hour + "小时" + minute + "分钟" + sec + "秒";
	}
}
/* Honey v3.1 Test - version:1 - OriginalChecksum:Be174122ba69631968a34fd618407d5eb (do not edit this line) */
